<?php

namespace App\Libraries;


trait Send
{

    public static $typeArray = [
        1 => 'user',
        2 => 'system',
    ];
    public static $moldArray = [
        1 => 'text',
        2 => 'img',
        3 => 'vocie',
        4 => 'video',
        5 => 'file',
        6 => 'weizhi'
    ];

    public static function creatSystem($text, $is = false)
    {
        if ($is) {
            $data = [
                'type' => 'group_content',
                'code' => 200,
                'msg' => [
                    'type' => 'text',
                    'content' => [
                        'text' => $text
                    ]
                ]
            ];
            return $data;
        }
        $data = [
            'type' => 'system',
            'code' => 200,
            'msg' => [
                'type' => 'text',
                'content' => [
                    'text' => $text
                ]
            ]
        ];
        return $data;
    }


    public static function createBox($list)
    {
        $data = [
            'type' => self::$typeArray[$list['type']],
            'code' => 200,
            'gid' => $list['gid'],
            'nid' => $list['nid'],
            'msg' => [
                'type' => self::$moldArray[$list['mold']],
                'userinfo' => [
                    'uid' => $list['uid'],
                    'username' => $list['username'],
                    'face' => $list['face']
                ],
                'content' => json_decode($list['content'], true)
            ]
        ];
        return $data;
    }

    public static function socket(int $type, int $msg_type, $content, $userinfo)
    {
        $typeArray = [
            1 => 'user',
            2 => 'system',
        ];
        $msgTypeArray = [
            1 => 'text',
            2 => 'img',
            3 => 'vocie',
            4 => 'video',
            5 => 'file',
            6 => 'weizhi'
        ];
        $data = [
            'type' => $typeArray[$type],
            'code' => 200,
            'msg' => [
                'type' => $msgTypeArray[$msg_type],
                'userinfo' => $userinfo,
                'content' => $content
            ]
        ];
        return json_encode($data, 256);
    }

    /**
     * Notes:返回成功消息
     */
    public static function successMsg($msg = '操作成功', $data = [], $code = 20000)
    {
        return self::returnMsg($data, $msg, $code);
    }

    /**
     * Notes:返回错误信息
     */
    public static function errorMsg($msg = '操作失败', $data = [], $code = 20001)
    {
        return self::returnMsg($data, $msg, $code);
    }

    public static function dieError($msg = '操作失败', $data = [], $code = 20001)
    {
        $data = [
            'msg' => $msg,
            'data' => $data,
            'code' => $code
        ];
        exit(json_encode($data, JSON_UNESCAPED_UNICODE));
    }

    /**
     * 返回成功
     */
    public static function returnMsg($data = [], $msg = '', $code = 20000)
    {
        return response()->json([
            'code' => $code,
            'msg' => $msg,
            'data' => isset($data) ? $data : [],
        ])->setEncodingOptions(JSON_UNESCAPED_UNICODE);
    }

    //请求正常，返回列表
    public static function returnList(array $list = [], int $total = 0)
    {
        $data = [
            'list' => $list,
            'total' => $total,
        ];
        return self::returnMsg($data);
    }

    //返回失败
    public static function returnError(int $code = 20001, string $msg = '')
    {
        return response()->json([
            'code' => $code,
            'msg' => $msg,
            'data' => [],
        ]);

    }
}

